#include "AlapForm.h"
#include <FIo.h>
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Locations;
using namespace Osp::Locations::Controls;
using namespace Osp::Locations::Services;
using namespace Osp::Media;
using namespace Osp::Graphics;
using namespace Osp::Io;

AlapForm::AlapForm(void)
{
}

AlapForm::~AlapForm(void)
{
}

bool
AlapForm::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_ALAPFORM");

	return true;
}

result
AlapForm::OnInitializing(void)
{
	result r = E_SUCCESS;

// softkey init and action binding

	this->SetSoftkeyText(SOFTKEY_0, "Zoom +");
	this->SetSoftkeyActionId(SOFTKEY_0,ID_ZOOM_IN);
	this->AddSoftkeyActionListener(SOFTKEY_0,*this);

	this->SetSoftkeyText(SOFTKEY_1, "Zoom -");
	this->SetSoftkeyActionId(SOFTKEY_1,ID_ZOOM_OUT);
	this->AddSoftkeyActionListener(SOFTKEY_1,*this);

// locationprovider init
	locProvider = new LocationProvider();
	locProvider->Construct(LOC_METHOD_HYBRID);
	locProvider->RequestLocationUpdates(*this, 5, false);

// map init
	String providerName = L"deCarta";
	String extraInfo = L"ClientName=yyy;ClientPassword=xxx;HostUrl=http://bada.developer.decarta.com/openls/openls";
	int width = 0;
	int height = 0;
	Coordinates center = Coordinates();
	center.Set(47.5, 19, 0);
	pMap = null;
	zoomLevel = 13;
	width=this->GetCanvasN()->GetBounds().width;
	height=this->GetCanvasN()->GetBounds().height;
//
	IMapServiceProvider* pProvider =
	         static_cast<IMapServiceProvider*>(ProviderManager::ConnectToServiceProviderN
	         (providerName, LOC_SVC_PROVIDER_TYPE_MAP, extraInfo));
//

	pMap = new Map();
	pMap->Construct(Osp::Graphics::Rectangle(0, 0, width, height), *pProvider);
	pMap->SetZoomLevel(zoomLevel, true);
	pMap->SetZoomEnabled(true);
	pMap->SetCenter(center, false);
	pMap->SetMyLocationEnabled(false);
	this->AddControl(*pMap);
	pMap->Draw();

// marker init
	Bitmap* bitmap;
	Image* bitmapDecoder = new Image();
	bitmapDecoder->Construct();
	bitmap = bitmapDecoder->DecodeN(L"/Res/mylogo.png", BITMAP_PIXEL_FORMAT_ARGB8888);

	Coordinates c;
	marker = new MapOverlayMarker(c);
	marker->SetImage(bitmap, *(new Point(bitmap->GetWidth()/2,bitmap->GetHeight())));
	marker->SetShadowEnabled(false);
	pMap->AddMapOverlay(*marker);
//
	delete bitmap;
	delete bitmapDecoder;

	return r;
}

result
AlapForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void
AlapForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
	case ID_ZOOM_IN:
		{
			AppLog("Zoom + \n");
			pMap->SetZoomLevel(++zoomLevel,true);
			pMap->Draw();
			this->RequestRedraw(true);
		}
		break;
	case ID_ZOOM_OUT:
		{
			AppLog("Zoom - \n");
			pMap->SetZoomLevel(--zoomLevel,true);
			pMap->Draw();
			this->RequestRedraw(true);
		}
		break;
	default:
		break;
	}
}

void
AlapForm::OnLocationUpdated(Location& location)
{
    if(location.GetQualifiedCoordinates()!=null)
    {
        const QualifiedCoordinates *q = location.GetQualifiedCoordinates();
        longitude = q->GetLongitude();
        latitude = q->GetLatitude();
		AppLog("%d %d", longitude, latitude);
		pMap->SetCenter(latitude,longitude,true);
		marker->SetCoordinates(*q);
		pMap->Draw();
        RequestRedraw(true);
    }
}

void
AlapForm::OnProviderStateChanged(LocProviderState newState){}
